package com.gitee.kinbug.sniper.web.config;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import com.gitee.kinbug.sniper.common.enums.CandleEnum;
import com.gitee.kinbug.sniper.common.enums.SymbolEnum;
import com.gitee.kinbug.sniper.common.util.Consts;
import com.gitee.kinbug.sniper.web.device.SeriesModel;
import com.gitee.kinbug.sniper.web.factory.SymbolFactory;
import com.gitee.kinbug.sniper.web.service.SeriesService;

@Configuration
public class ServerConfig implements InitializingBean {

	ExecutorService exe = new ThreadPoolExecutor(16, 16, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(100), new ThreadPoolExecutor.CallerRunsPolicy());

	// 要关心的币对
	public final static List<SymbolEnum> SYMBOLS = Arrays.asList(SymbolEnum.BTC);

	/**
	 * -计算K线指标
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		for (SymbolEnum symbol : SYMBOLS) {
			SeriesService seriesService = new SeriesService(symbol);
			exe.execute(new Runnable() {
				@Override
				public void run() {
					seriesService.compute(Consts.CANDLES);
				}
			});
		}
	}

	// cron表达式：每隔5秒执行一次
	@Scheduled(cron = "0/3 * * * * *")
	public void scheduled() {
		SeriesModel seriesModel = SymbolFactory.get(SymbolEnum.BTC).getSeriesModel();
		try {
			Thread.sleep(120);
			seriesModel.getIndex().getBoll(CandleEnum.MIN5);
			System.err.println(seriesModel.buyDnRiskPts() + " " + seriesModel.buyUpRiskPts());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
